package jinyilw.swing.visuals.window;

import jinyilw.common.Operator;
import jinyilw.swing.Direction;
import jinyilw.swing.DragCompUtils;
import jinyilw.swing.FontTools;
import jinyilw.swing.WindowTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.rlaf.theme.RColorSet;
import jinyilw.swing.visuals.Icomp.IButton;
import jinyilw.swing.visuals.Icomp.ILabel;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

public class ITipWindow extends TransWindow // 提示窗
{
	private static final long serialVersionUID = -8449534238834001142L;
	ICSetting[] tip;
	int tipCount = 0;
	Operator<?> finishOperator;
	Window owner;
	Point location;
	ILabel tipLabel = new ILabel();
	JLabel textLabel = new JLabel();
	JLabel closeLabel = new JLabel();
	IButton okButton = new IButton();
	IButton nButton = new IButton();
	Shape windowShape;

	public ITipWindow(ICSetting[] str, Window window)
	{
		super(window);
		tip = str;
		owner = window;
		DragCompUtils.setMovable(this, this);
		location = owner.getLocation();
		owner.addComponentListener(new LocationListener());
		setAlwaysOnTop(true);
		setSize(250, 95);
		windowShape = new RoundRectangle2D.Double(1, 1, 248, 93, 120, 90);
		tipLabel.setHorizontalAlignment(SwingConstants.CENTER);
		tipLabel.setForeground(Color.RED);
		tipLabel.setBounds(1, 30, 248, 26);
		tipLabel.setText(tip[0]);
		textLabel.setBounds(85, 7, 65, 17);
		textLabel.setText("Tip " + 1 + "/" + tip.length);
		textLabel.setForeground(Color.black);
		textLabel.setFont(FontTools.getSystemFont(12));
		okButton.setForeground(new Color(0, 128, 0));
		okButton.setFont(FontTools.getCustomFont(13));
		okButton.setBackground(Color.WHITE);
		okButton.addActionListener(new OKButtonActionListener());
		okButton.setText(new ICSetting("ok", "确 定", "OK"), true);
		okButton.setBounds(50, 61, 60, 22);
		okButton.setDefaultAlpha(0.55f);
		nButton.setText(new ICSetting("noShow", "不再显示", "Do not show"), true);
		nButton.setForeground(new Color(0, 128, 0));
		nButton.setBackground(Color.WHITE);
		nButton.setBounds(115, 61, 78, 22);
		nButton.setDefaultAlpha(0.55f);
		closeLabel.addMouseListener(new CloseLabelMouseListener());
		closeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		closeLabel.setFont(FontTools.getSystemFont(18));
		closeLabel.setForeground(Color.black);
		closeLabel.setText("×");
		closeLabel.setBounds(150, 7, 17, 17);
		closeLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		add(tipLabel);
		add(okButton);
		add(nButton);
		add(textLabel);
		add(closeLabel);
	}

	public void addFinishActionListener(ActionListener listener)
	{
		nButton.addActionListener(listener);
	}

	public void showInitTipWindow()
	{
		WindowTools.showTipWindow(this, owner, Direction.DOWN);
	}

	public void showTipWindow()
	{
		WindowTools.showTipWindow(this, this, Direction.DOWN);
	}

	@Override
	protected void paintBackground(Graphics g, int width, int height)
	{
		Graphics2D g2d = (Graphics2D) g.create();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(RColorSet.lightTransBGColor);
		g2d.fill(windowShape);
		g2d.setColor(RColorSet.lightBorderColor);
		g2d.draw(windowShape);
		g2d.dispose();
	}

	private class OKButtonActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			tipCount++;
			if (tipCount < tip.length)
			{
				tipLabel.setText(tip[tipCount]);
				textLabel.setText("Tip " + (tipCount + 1) + "/" + tip.length);
				showTipWindow();
			} else
			{
				tipCount = 0;
				tipLabel.setText(tip[tipCount]);
				textLabel.setText("Tip " + (tipCount + 1) + "/" + tip.length);
				dispose();
				if (finishOperator != null)
					finishOperator.operate(null);
			}
		}
	}

	private class CloseLabelMouseListener extends MouseAdapter
	{
		@Override
		public void mouseClicked(final MouseEvent e)
		{
			dispose();
		}
	}

	private class LocationListener extends ComponentAdapter
	{
		@Override
		public void componentMoved(final ComponentEvent e)
		{
			Point newLocation = owner.getLocation();
			int x = newLocation.x - location.x;
			int y = newLocation.y - location.y;
			ITipWindow.this.setLocation(ITipWindow.this.getLocation().x + x,
					ITipWindow.this.getLocation().y + y);
			location = newLocation;
		}
	}
}
